home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / Print.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  24KB  |  1,140 lines

  1. /*
  2. **    Print.c
  3. **
  4. **    Printer control routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* PrintText(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR String,...):
  17.      *
  18.      *    Output a printf() style message.
  19.      */
  20.  
  21. BOOL
  22. PrintText(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR String,...)
  23. {
  24.     UBYTE LocalBuffer[256];
  25.     va_list    VarArgs;
  26.     LONG Len;
  27.  
  28.     va_start(VarArgs,String);
  29.     LimitedVSPrintf(sizeof(LocalBuffer),LocalBuffer,String,VarArgs);
  30.     va_end(VarArgs);
  31.  
  32.     if(ReqWindow)
  33.     {
  34.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  35.         {
  36.             *Error = 0;
  37.  
  38.             return(FALSE);
  39.         }
  40.     }
  41.     else
  42.     {
  43.         if(SetSignal(0,SIG_BREAK) & SIG_BREAK)
  44.         {
  45.             *Error = 0;
  46.  
  47.             return(FALSE);
  48.         }
  49.     }
  50.  
  51.     Len = strlen(LocalBuffer) + 1;
  52.  
  53.     SetIoErr(0);
  54.  
  55.     LimitedStrcat(sizeof(LocalBuffer),LocalBuffer,"\n");
  56.  
  57.     if(Write(File,LocalBuffer,Len) < Len)
  58.     {
  59.         *Error = IoErr();
  60.  
  61.         return(FALSE);
  62.     }
  63.     else
  64.         return(TRUE);
  65. }
  66.  
  67.     /* PrintHeader(BPTR File,struct Window *ReqWindow,LONG *Error,ULONG Code):
  68.      *
  69.      *    Print a line header.
  70.      */
  71.  
  72. STATIC BOOL
  73. PrintHeader(BPTR File,struct Window *ReqWindow,LONG *Error,ULONG Code,BOOL Plain)
  74. {
  75.     UBYTE LocalBuffer[256];
  76.     STRPTR String;
  77.     LONG Len;
  78.  
  79.     String = LocaleString(Code);
  80.  
  81.     if(ReqWindow)
  82.     {
  83.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  84.         {
  85.             *Error = 0;
  86.  
  87.             return(FALSE);
  88.         }
  89.     }
  90.     else
  91.     {
  92.         if(SetSignal(0,SIG_BREAK) & SIG_BREAK)
  93.         {
  94.             *Error = 0;
  95.  
  96.             return(FALSE);
  97.         }
  98.     }
  99.  
  100.     if(!Plain)
  101.     {
  102.         LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,"\33[1m%s\33[0m",String);
  103.         String = LocalBuffer;
  104.     }
  105.  
  106.     SetIoErr(0);
  107.  
  108.     Len = strlen(String);
  109.  
  110.     if(Write(File,String,Len) < Len)
  111.     {
  112.         *Error = IoErr();
  113.  
  114.         return(FALSE);
  115.     }
  116.  
  117.     return(TRUE);
  118. }
  119.  
  120.     /* PrintFileInformation():
  121.      *
  122.      *    Print information on a file.
  123.      */
  124.  
  125. BOOL
  126. PrintFileInformation(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR Name,ULONG Flags)
  127. {
  128.     BOOL Continue;
  129.  
  130.         /* Any special information to print along with the name? */
  131.  
  132.     if(Flags)
  133.     {
  134.         BPTR FileLock;
  135.  
  136.             /* Try to grip the file. */
  137.  
  138.         if(FileLock = Lock(Name,ACCESS_READ))
  139.         {
  140.             D_S(struct FileInfoBlock,FileInfo);
  141.  
  142.                 /* How does it look like? */
  143.  
  144.             if(Examine(FileLock,FileInfo))
  145.             {
  146.                 UBYTE DummyBuffer[300];
  147.                 STRPTR Index;
  148.  
  149.                     /* Add the size. */
  150.  
  151.                 if(Flags & PRINT_SIZE)
  152.                     LimitedSPrintf(sizeof(DummyBuffer),DummyBuffer,"%-25s %7ld",FilePart(Name),FileInfo->fib_Size);
  153.                 else
  154.                     LimitedSPrintf(sizeof(DummyBuffer),DummyBuffer,"%-25s",FilePart(Name));
  155.  
  156.                 Index = DummyBuffer;
  157.  
  158.                     /* Find the end of the string. */
  159.  
  160.                 while(*Index)
  161.                     Index++;
  162.  
  163.                     /* Add the protection bits. */
  164.  
  165.                 if(Flags & PRINT_BITS)
  166.                 {
  167.                     STATIC STRPTR    SetBits = "----aps",
  168.                                     ClrBits = "dewr---";
  169.  
  170.                     UBYTE TempString[10];
  171.  
  172.                     LONG i;
  173.  
  174.                     strcpy(TempString," -------");
  175.  
  176.                     for(i = 0 ; i < 7 ; i++)
  177.                     {
  178.                         if(FileInfo->fib_Protection & (1L << i))
  179.                             TempString[6 - i + 1] = SetBits[i];
  180.                         else
  181.                             TempString[6 - i + 1] = ClrBits[i];
  182.                     }
  183.  
  184.                     strcpy(Index,TempString);
  185.  
  186.                     while(*Index)
  187.                         Index++;
  188.                 }
  189.  
  190.                     /* Add the creation date. */
  191.  
  192.                 if(Flags & PRINT_DATE)
  193.                 {
  194.                     UBYTE Date[20],Time[20];
  195.                     struct DateTime    DateTime;
  196.  
  197.                         /* Prepare for date conversion. */
  198.  
  199.                     DateTime.dat_Stamp        = FileInfo->fib_Date;
  200.                     DateTime.dat_Format        = FORMAT_DEF;
  201.                     DateTime.dat_Flags        = DTF_SUBST;
  202.                     DateTime.dat_StrDay        = NULL;
  203.                     DateTime.dat_StrDate    = Date;
  204.                     DateTime.dat_StrTime    = Time;
  205.  
  206.                         /* Convert the date. */
  207.  
  208.                     if(DateToStr(&DateTime))
  209.                     {
  210.                         LimitedSPrintf(sizeof(DummyBuffer) - ((LONG)Index - (LONG)DummyBuffer),Index," %-9s %s",Date,Time);
  211.  
  212.                         while(*Index)
  213.                             Index++;
  214.                     }
  215.                 }
  216.  
  217.                     /* Add the file comment. */
  218.  
  219.                 if(Flags & PRINT_COMMENT)
  220.                     LimitedSPrintf(sizeof(DummyBuffer) - ((LONG)Index - (LONG)DummyBuffer),Index,"\n: %s",FileInfo->fib_Comment);
  221.  
  222.                 Continue = PrintText(File,ReqWindow,Error,"%s\n",DummyBuffer);
  223.             }
  224.             else
  225.                 Continue = FALSE;
  226.  
  227.             UnLock(FileLock);
  228.         }
  229.         else
  230.             Continue = FALSE;
  231.     }
  232.     else
  233.         Continue = PrintText(File,ReqWindow,Error,"%s\n",Name);
  234.  
  235.     return(Continue);
  236. }
  237.  
  238.     /* PrintEntry(BPTR File,struct Window *ReqWindow,LONG *Error,struct PhoneEntry *Entry):
  239.      *
  240.      *    Print information on the contents of a phonebook entry.
  241.      */
  242.  
  243. BOOL
  244. PrintEntry(BPTR File,struct Window *ReqWindow,BOOL Plain,LONG *Error,struct PhoneEntry *Entry,ULONG Flags)
  245. {
  246.     if(Plain)
  247.     {
  248.         if(!PrintText(File,ReqWindow,Error,"\n\"%s\" (%s)",Entry->Header->Name,Entry->Header->Number))
  249.             return(FALSE);
  250.     }
  251.     else
  252.     {
  253.         if(!PrintText(File,ReqWindow,Error,"\n\33[4m\"%s\" (%s)\33[0m",Entry->Header->Name,Entry->Header->Number))
  254.             return(FALSE);
  255.     }
  256.  
  257.     if(Flags & PRINT_COMMENT)
  258.     {
  259.         if(Entry->Header->Comment[0])
  260.         {
  261.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_COMMENT_TXT,Plain))
  262.                 return(FALSE);
  263.  
  264.             if(!PrintText(File,ReqWindow,Error,Entry->Header->Comment))
  265.                 return(FALSE);
  266.         }
  267.     }
  268.  
  269.     if(Flags & PRINT_USERNAME)
  270.     {
  271.         if(Entry->Header->UserName[0])
  272.         {
  273.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_USER_NAME_TXT,Plain))
  274.                 return(FALSE);
  275.  
  276.             if(!PrintText(File,ReqWindow,Error,Entry->Header->UserName))
  277.                 return(FALSE);
  278.         }
  279.     }
  280.  
  281.     if((Flags & PRINT_SERIAL) && Entry->Config->SerialConfig)
  282.     {
  283.         STATIC UBYTE Parities[] =
  284.         {
  285.             'N','E','O','M','S'
  286.         };
  287.  
  288.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_BAUD_RATE_TXT,Plain))
  289.             return(FALSE);
  290.  
  291.         if(LocaleBase)
  292.         {
  293.             if(!PrintText(File,ReqWindow,Error,"%lD",Entry->Config->SerialConfig->BaudRate))
  294.                 return(FALSE);
  295.         }
  296.         else
  297.         {
  298.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry->Config->SerialConfig->BaudRate))
  299.                 return(FALSE);
  300.         }
  301.  
  302.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_PARAMETERS_TXT,Plain))
  303.             return(FALSE);
  304.  
  305.         if(!PrintText(File,ReqWindow,Error,"%ld-%lc-%ld",Entry->Config->SerialConfig->BitsPerChar,Parities[(WORD)Entry->Config->SerialConfig->Parity],Entry->Config->SerialConfig->StopBits))
  306.             return(FALSE);
  307.  
  308.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_HANDSHAKING_TXT,Plain))
  309.             return(FALSE);
  310.  
  311.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SERIALPANEL_HANDSHAKING_NONE_TXT + Entry->Config->SerialConfig->HandshakingProtocol)))
  312.             return(FALSE);
  313.  
  314.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DUPLEX_TXT,Plain))
  315.             return(FALSE);
  316.  
  317.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SERIALPANEL_DUPLEX_FULL_TXT + Entry->Config->SerialConfig->Duplex)))
  318.             return(FALSE);
  319.  
  320.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_STRIP_BIT_TXT,Plain))
  321.             return(FALSE);
  322.  
  323.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->SerialConfig->StripBit8)))
  324.             return(FALSE);
  325.  
  326.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_FLOW_CONTROL_TXT,Plain))
  327.             return(FALSE);
  328.  
  329.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->SerialConfig->xONxOFF)))
  330.             return(FALSE);
  331.  
  332.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_SERIAL_DRIVER_TXT,Plain))
  333.             return(FALSE);
  334.  
  335.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_NAME_UNIT_TEMPLATE_TXT),Entry->Config->SerialConfig->SerialDevice, + Entry->Config->SerialConfig->UnitNumber))
  336.             return(FALSE);
  337.     }
  338.  
  339.     if((Flags & PRINT_MODEM) && Entry->Config->ModemConfig)
  340.     {
  341.         if(Entry->Config->ModemConfig->ModemInit[0])
  342.         {
  343.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_MODEM_INIT_TXT,Plain))
  344.                 return(FALSE);
  345.  
  346.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->ModemInit))
  347.                 return(FALSE);
  348.         }
  349.  
  350.         if(Entry->Config->ModemConfig->ModemExit[0])
  351.         {
  352.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_MODEM_EXIT_TXT,Plain))
  353.                 return(FALSE);
  354.  
  355.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->ModemExit))
  356.                 return(FALSE);
  357.         }
  358.  
  359.         if(Entry->Config->ModemConfig->ModemHangup[0])
  360.         {
  361.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_HANG_UP_TXT,Plain))
  362.                 return(FALSE);
  363.  
  364.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->ModemHangup))
  365.                 return(FALSE);
  366.         }
  367.  
  368.         if(Entry->Config->ModemConfig->DialPrefix[0])
  369.         {
  370.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_PREFIX_TXT,Plain))
  371.                 return(FALSE);
  372.  
  373.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->DialPrefix))
  374.                 return(FALSE);
  375.         }
  376.  
  377.         if(Entry->Config->ModemConfig->DialSuffix[0])
  378.         {
  379.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_SUFFIX_TXT,Plain))
  380.                 return(FALSE);
  381.  
  382.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->DialSuffix))
  383.                 return(FALSE);
  384.         }
  385.  
  386.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_REDIAL_DELAY_TXT,Plain))
  387.             return(FALSE);
  388.  
  389.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MINUTE_SECOND_TEMPLATE_TXT),Entry->Config->ModemConfig->RedialDelay / 60,Entry->Config->ModemConfig->RedialDelay % 60))
  390.             return(FALSE);
  391.  
  392.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_TIMEOUT_TXT,Plain))
  393.             return(FALSE);
  394.  
  395.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MINUTE_SECOND_TEMPLATE_TXT),Entry->Config->ModemConfig->DialTimeout / 60,Entry->Config->ModemConfig->DialTimeout % 60))
  396.             return(FALSE);
  397.  
  398.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_AUTO_BAUD_TXT,Plain))
  399.             return(FALSE);
  400.  
  401.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->ModemConfig->ConnectAutoBaud)))
  402.             return(FALSE);
  403.  
  404.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DROP_DTR_TXT,Plain))
  405.             return(FALSE);
  406.  
  407.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->ModemConfig->DropDTR)))
  408.             return(FALSE);
  409.     }
  410.  
  411.     if((Flags & PRINT_SCREEN) && Entry->Config->ScreenConfig)
  412.     {
  413.         UBYTE ModeNameBuffer[DISPLAYNAMELEN + 1];
  414.  
  415.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DISPLAY_MODE_TXT,Plain))
  416.             return(FALSE);
  417.  
  418.         GetModeName(Entry->Config->ScreenConfig->DisplayMode,ModeNameBuffer,sizeof(ModeNameBuffer));
  419.  
  420.         if(!PrintText(File,ReqWindow,Error,ModeNameBuffer))
  421.             return(FALSE);
  422.  
  423.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_COLOUR_MODE_TXT,Plain))
  424.             return(FALSE);
  425.  
  426.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SCREENPANEL_COLOUR_AMIGA_TXT + Entry->Config->ScreenConfig->ColourMode)))
  427.             return(FALSE);
  428.     }
  429.  
  430.     if((Flags & PRINT_TERMINAL) && Entry->Config->TerminalConfig)
  431.     {
  432.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TERMINAL_EMULATION_TXT,Plain))
  433.             return(FALSE);
  434.  
  435.         if(Entry->Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  436.         {
  437.             if(!PrintText(File,ReqWindow,Error,"%s, \"%s\"",LocaleString(MSG_TERMINALPANEL_EMULATION_ANSI_VT102_TXT + Entry->Config->TerminalConfig->EmulationMode),Entry->Config->TerminalConfig->EmulationMode))
  438.                 return(FALSE);
  439.         }
  440.         else
  441.         {
  442.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_TERMINALPANEL_EMULATION_ANSI_VT102_TXT + Entry->Config->TerminalConfig->EmulationMode)))
  443.                 return(FALSE);
  444.         }
  445.  
  446.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_FONT_TXT,Plain))
  447.             return(FALSE);
  448.  
  449.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_TERMINALPANEL_FONT_STANDARD_TXT + Entry->Config->TerminalConfig->FontMode)))
  450.             return(FALSE);
  451.  
  452.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TEXT_COLUMNS_TXT,Plain))
  453.             return(FALSE);
  454.  
  455.         if(Entry->Config->TerminalConfig->NumColumns < 20)
  456.         {
  457.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MAXIMUM_TXT)))
  458.                 return(FALSE);
  459.         }
  460.         else
  461.         {
  462.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry->Config->TerminalConfig->NumColumns))
  463.                 return(FALSE);
  464.         }
  465.  
  466.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TEXT_LINES_TXT,Plain))
  467.             return(FALSE);
  468.  
  469.         if(Entry->Config->TerminalConfig->NumLines < 20)
  470.         {
  471.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MAXIMUM_TXT)))
  472.                 return(FALSE);
  473.         }
  474.         else
  475.         {
  476.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry->Config->TerminalConfig->NumLines))
  477.                 return(FALSE);
  478.         }
  479.  
  480.         if(Entry->Config->TerminalConfig->KeyMapFileName[0])
  481.         {
  482.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_KEYMAP_FILE_TXT,Plain))
  483.                 return(FALSE);
  484.  
  485.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->TerminalConfig->KeyMapFileName))
  486.                 return(FALSE);
  487.         }
  488.     }
  489.  
  490.     return(TRUE);
  491. }
  492.  
  493.     /* PrintScreen(BPTR File,struct Window *ReqWindow,LONG *Error):
  494.      *
  495.      *    Print the contents of the screen, requires the raster
  496.      *    to be available.
  497.      */
  498.  
  499. BOOL
  500. PrintScreen(BPTR File,struct Window *ReqWindow,LONG *Error)
  501. {
  502.     UBYTE *Buffer;
  503.     LONG i,j;
  504.  
  505.         /* Run down the lines... */
  506.  
  507.     for(i = 0 ; i <= LastLine ; i++)
  508.     {
  509.             /* Grab the line. */
  510.  
  511.         Buffer = &Raster[i * RasterWidth];
  512.  
  513.         j = LastColumn;
  514.  
  515.             /* Strip trailing spaces. */
  516.  
  517.         while(j >= 0 && Buffer[j] == ' ')
  518.             j--;
  519.  
  520.             /* Blank line? */
  521.  
  522.         if(j >= 0)
  523.         {
  524.             SetIoErr(0);
  525.  
  526.             if(Write(File,Buffer,j + 1) < j + 1)
  527.             {
  528.                 *Error = IoErr();
  529.  
  530.                 return(FALSE);
  531.             }
  532.         }
  533.  
  534.             /* Is printing to be aborted? */
  535.  
  536.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  537.         {
  538.             *Error = 0;
  539.  
  540.             return(FALSE);
  541.         }
  542.  
  543.             /* Add line terminator. */
  544.  
  545.         SetIoErr(0);
  546.  
  547.         if(Write(File,"\n",1) < 1)
  548.         {
  549.             *Error = IoErr();
  550.  
  551.             return(FALSE);
  552.         }
  553.  
  554.             /* Is printing to be aborted? */
  555.  
  556.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  557.         {
  558.             *Error = 0;
  559.  
  560.             return(FALSE);
  561.         }
  562.     }
  563.  
  564.     return(TRUE);
  565. }
  566.  
  567.     /* PrintClip(BPTR File,struct Window *ReqWindow,LONG *Error):
  568.      *
  569.      *    Print the contents of the clipboard.
  570.      */
  571.  
  572. BOOL
  573. PrintClip(BPTR File,struct Window *ReqWindow,LONG *Error)
  574. {
  575.         /* Are we currently reading input from the
  576.          * clipboard? If so, close it.
  577.          */
  578.  
  579.     if(ClipInput)
  580.     {
  581.         CloseClip();
  582.  
  583.         ClipInput = ClipXerox = FALSE;
  584.     }
  585.  
  586.         /* Open the clipboard for reading. */
  587.  
  588.     if(*Error = OpenClip(Config->ClipConfig->ClipboardUnit))
  589.         return(FALSE);
  590.     else
  591.     {
  592.         UBYTE InputBuffer[256];
  593.         LONG Len;
  594.  
  595.             /* Read clipboard contents. */
  596.  
  597.         while((Len = GetClip(InputBuffer,sizeof(InputBuffer) - 1)) > 0)
  598.         {
  599.                 /* Are we to stop printing? */
  600.  
  601.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  602.             {
  603.                 *Error = 0;
  604.  
  605.                 CloseClip();
  606.  
  607.                 return(FALSE);
  608.             }
  609.             else
  610.             {
  611.                 SetIoErr(0);
  612.  
  613.                 if(Write(File,InputBuffer,Len) < Len)
  614.                 {
  615.                     *Error = IoErr();
  616.  
  617.                     CloseClip();
  618.  
  619.                     return(FALSE);
  620.                 }
  621.             }
  622.         }
  623.  
  624.         if(Len < 0)
  625.         {
  626.             if(SysReqHandler(ReqWindow,NULL,FALSE) == -2)
  627.             {
  628.                 SetIoErr(0);
  629.  
  630.                 if(Write(File,"\n",1) < 1)
  631.                     *Error = IoErr();
  632.  
  633.                 CloseClip();
  634.  
  635.                 return(FALSE);
  636.             }
  637.         }
  638.  
  639.         CloseClip();
  640.     }
  641.  
  642.     return(TRUE);
  643. }
  644.  
  645.     /* PrintBuffer(BPTR File,struct Window *ReqWindow,LONG *Error):
  646.      *
  647.      *    Print the contents of the text buffer.
  648.      */
  649.  
  650. BOOL
  651. PrintBuffer(BPTR File,struct Window *ReqWindow,LONG *Error)
  652. {
  653.     BOOL Continue;
  654.     LONG i,Len;
  655.  
  656.     SafeObtainSemaphoreShared(&BufferSemaphore);
  657.  
  658.     Continue = TRUE;
  659.  
  660.     if(BufferLines)
  661.     {
  662.         for(i = 0 ; i < Lines ; i++)
  663.         {
  664.             Len = BufferLines[i][-1];
  665.  
  666.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  667.             {
  668.                 *Error = 0;
  669.  
  670.                 Continue = FALSE;
  671.  
  672.                 break;
  673.             }
  674.  
  675.             if(Len)
  676.             {
  677.                 SetIoErr(0);
  678.  
  679.                 if(Write(File,BufferLines[i],Len) < Len)
  680.                 {
  681.                     *Error = IoErr();
  682.  
  683.                     Continue = FALSE;
  684.  
  685.                     break;
  686.                 }
  687.             }
  688.  
  689.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  690.             {
  691.                 *Error = 0;
  692.  
  693.                 Continue = FALSE;
  694.  
  695.                 break;
  696.             }
  697.  
  698.             SetIoErr(0);
  699.  
  700.             if(Write(File,"\n",1) < 1)
  701.             {
  702.                 *Error = IoErr();
  703.  
  704.                 Continue = FALSE;
  705.  
  706.                 break;
  707.             }
  708.         }
  709.     }
  710.     else
  711.         Continue = FALSE;
  712.  
  713.     ReleaseSemaphore(&BufferSemaphore);
  714.  
  715.     return(Continue);
  716. }
  717.  
  718.     /* PrintSomething(BYTE Source):
  719.      *
  720.      *    Print the screen or the current contents of the clipboard.
  721.      */
  722.  
  723. VOID
  724. PrintSomething(LONG Source)
  725. {
  726.     struct Window *ReqWindow;
  727.     struct EasyStruct Easy;
  728.     LONG Error;
  729.  
  730.         /* Fill in the easy requester structure. */
  731.  
  732.     Easy.es_StructSize        = sizeof(struct EasyStruct);
  733.     Easy.es_Flags            = NULL;
  734.     Easy.es_Title            = (STRPTR)LocaleString(MSG_TERMAUX_TERM_REQUEST_TXT);
  735.     Easy.es_GadgetFormat    = (STRPTR)LocaleString(MSG_PRINT_STOP_TXT);
  736.  
  737.     if(Source == PRINT_CLIP)
  738.         Easy.es_TextFormat = (STRPTR)LocaleString(MSG_PRINT_PRINTING_CLIP_TXT);
  739.     else
  740.         Easy.es_TextFormat = (STRPTR)LocaleString(MSG_PRINT_PRINTING_SCREEN_TXT);
  741.  
  742.         /* The requester is to be displayed while printing. */
  743.  
  744.     if(ReqWindow = BuildEasyRequest(Window,&Easy,NULL))
  745.     {
  746.         BPTR SomeFile;
  747.  
  748.             /* Add header information if printer channel is already open. */
  749.  
  750.         if(PrinterCapture)
  751.         {
  752.             LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT));
  753.  
  754.             if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT),Len) < Len)
  755.             {
  756.                 FreeSysRequest(ReqWindow);
  757.  
  758.                 ReqWindow = NULL;
  759.  
  760.                 Error = IoErr();
  761.  
  762.                 SomeFile = NULL;
  763.             }
  764.             else
  765.                 SomeFile = PrinterCapture;
  766.         }
  767.         else
  768.         {
  769.                 /* Open printer channel. */
  770.  
  771.             if(!(SomeFile = Open("PRT:",MODE_NEWFILE)))
  772.             {
  773.                 FreeSysRequest(ReqWindow);
  774.  
  775.                 ReqWindow = NULL;
  776.  
  777.                 Error = IoErr();
  778.             }
  779.         }
  780.  
  781.             /* Everything fine so far? */
  782.  
  783.         if(!Error && SomeFile)
  784.         {
  785.             BOOL Stopped;
  786.  
  787.                 /* Are we to print the screen? */
  788.  
  789.             if(Source == PRINT_SCREEN)
  790.                 Stopped = !PrintScreen(SomeFile,ReqWindow,&Error);
  791.             else
  792.                 Stopped = !PrintClip(SomeFile,ReqWindow,&Error);
  793.  
  794.                 /* Add a trailer if necessary. */
  795.  
  796.             if(PrinterCapture)
  797.             {
  798.                 if(!Error && !Stopped)
  799.                 {
  800.                     LONG Len;
  801.  
  802.                     SetIoErr(0);
  803.  
  804.                     Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT));
  805.  
  806.                     if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT),Len) < Len)
  807.                         Error = IoErr();
  808.                 }
  809.             }
  810.             else
  811.             {
  812.                     /* Close the printer stream. */
  813.  
  814.                 if(!Close(SomeFile))
  815.                     Error = IoErr();
  816.             }
  817.         }
  818.  
  819.             /* Release the system requster. */
  820.  
  821.         if(ReqWindow)
  822.             FreeSysRequest(ReqWindow);
  823.  
  824.             /* Display the error code if necessary. */
  825.  
  826.         if(Error)
  827.         {
  828.             UBYTE LocalBuffer[256];
  829.             STRPTR ErrorString;
  830.  
  831.             if(Fault(Error,NULL,LocalBuffer,sizeof(LocalBuffer)))
  832.                 ErrorString = LocalBuffer;
  833.             else
  834.                 ErrorString = "???";
  835.  
  836.             ShowRequest(Window,LocaleString(MSG_PRINT_ERROR_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error,ErrorString);
  837.         }
  838.     }
  839. }
  840.  
  841.     /* PrintRegion(LONG Top,LONG Bottom):
  842.      *
  843.      *    Print the contents of a screen region.
  844.      */
  845.  
  846. VOID
  847. PrintRegion(LONG Top,LONG Bottom,BOOL FormFeed)
  848. {
  849.     BPTR SomeFile;
  850.     LONG i,j;
  851.     UBYTE *Buffer;
  852.  
  853.     BlockWindows();
  854.  
  855.     if(PrinterCapture)
  856.     {
  857.         LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT));
  858.  
  859.         if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT),Len) < Len)
  860.         {
  861.             ShowRequest(Window,LocaleString(MSG_CONSOLE_ERROR_WRITING_TO_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT));
  862.  
  863.             ReleaseWindows();
  864.  
  865.             return;
  866.         }
  867.  
  868.         SomeFile = PrinterCapture;
  869.     }
  870.     else
  871.     {
  872.         if(!(SomeFile = Open("PRT:",MODE_NEWFILE)))
  873.         {
  874.             ShowRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_OPEN_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT));
  875.  
  876.             ReleaseWindows();
  877.  
  878.             return;
  879.         }
  880.     }
  881.  
  882.     for(i = Top ; i < Bottom ; i++)
  883.     {
  884.         Buffer = &Raster[i * RasterWidth];
  885.  
  886.         j = LastColumn;
  887.  
  888.         while(j >= 0 && Buffer[j] == ' ')
  889.             j--;
  890.  
  891.         if(j >= 0)
  892.         {
  893.             SetIoErr(0);
  894.  
  895.             if(Write(SomeFile,Buffer,j + 1) < j + 1)
  896.             {
  897.                 FormFeed = FALSE;
  898.  
  899.                 break;
  900.             }
  901.         }
  902.  
  903.         SetIoErr(0);
  904.  
  905.         if(Write(SomeFile,"\n",1) < 1)
  906.         {
  907.             FormFeed = FALSE;
  908.  
  909.             break;
  910.         }
  911.     }
  912.  
  913.     if(PrinterCapture)
  914.     {
  915.         LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT));
  916.  
  917.         Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT),Len);
  918.     }
  919.     else
  920.     {
  921.         if(FormFeed)
  922.             Write(SomeFile,"\f",1);
  923.  
  924.         Close(SomeFile);
  925.     }
  926.  
  927.     ReleaseWindows();
  928.  
  929. }
  930.  
  931.     /* PrintScreenGfx():
  932.      *
  933.      *    Print the window contents as graphics.
  934.      */
  935.  
  936. BOOL
  937. PrintScreenGfx()
  938. {
  939.     struct MsgPort *PrintPort;
  940.     LONG Error;
  941.  
  942.         /* Create the printer port */
  943.  
  944.     if(PrintPort = CreateMsgPort())
  945.     {
  946.         struct IODRPReq *PrintRequest;
  947.  
  948.             /* Create the rastport dump request */
  949.  
  950.         if(PrintRequest = (struct IODRPReq *)CreateIORequest(PrintPort,sizeof(struct IODRPReq)))
  951.         {
  952.                 /* Open the printer driver */
  953.  
  954.             if(!OpenDevice("printer.device",0,(struct IORequest *)PrintRequest,NULL))
  955.             {
  956.                 struct RastPort *RPort;
  957.  
  958.                     /* Create a new rastport */
  959.  
  960.                 if(RPort = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY))
  961.                 {
  962.                     struct BitMap *BitMap;
  963.                     LONG Width,Height;
  964.  
  965.                         /* Initialize the rastport */
  966.  
  967.                     InitRastPort(RPort);
  968.  
  969.                         /* Keep these handy */
  970.  
  971.                     Width    = Window->Width        - (Window->BorderLeft + Window->BorderRight);
  972.                     Height    = Window->Height    - (Window->BorderTop + Window->BorderBottom);
  973.  
  974.                     if(StatusWindow)
  975.                         Height -= StatusDisplayHeight;
  976.  
  977.                         /* Allocate offscreen buffer to hold the window contents */
  978.  
  979.                     if(BitMap = CreateBitMap(Width,Height,GetBitMapDepth(Window->RPort->BitMap),NULL,Window->RPort->BitMap))
  980.                     {
  981.                         struct EasyStruct Easy;
  982.                         struct Window *ReqWindow;
  983.  
  984.                             /* Put the bitmap into the rastport */
  985.  
  986.                         RPort->BitMap = BitMap;
  987.  
  988.                             /* Clear the bitmap */
  989.  
  990.                         SetRast(RPort,0);
  991.  
  992.                             /* Copy the window contents to the offscreen buffer */
  993.  
  994.                         ClipBlit(Window->RPort,Window->BorderLeft,Window->BorderTop,RPort,0,0,Width,Height,MINTERM_COPY);
  995.  
  996.                             /* Wait for the bitmap to be transferred */
  997.  
  998.                         WaitBlit();
  999.  
  1000.                             /* Set up the print request */
  1001.  
  1002.                         PrintRequest->io_Command    = PRD_DUMPRPORT;
  1003.                         PrintRequest->io_RastPort    = RPort;
  1004.                         PrintRequest->io_ColorMap    = Window->WScreen->ViewPort.ColorMap;
  1005.                         PrintRequest->io_Modes        = GetVPModeID(&Window->WScreen->ViewPort);
  1006.                         PrintRequest->io_SrcWidth    = Width;
  1007.                         PrintRequest->io_SrcHeight    = Height;
  1008.  
  1009.                             /* Set up the abort requester */
  1010.  
  1011.                         Easy.es_StructSize        = sizeof(Easy);
  1012.                         Easy.es_Flags            = NULL;
  1013.                         Easy.es_Title            = LocaleString(MSG_TERMAUX_TERM_REQUEST_TXT);
  1014.                         Easy.es_GadgetFormat    = LocaleString(MSG_GLOBAL_ABORT_GAD);
  1015.                         Easy.es_TextFormat        = LocaleString(MSG_PRINTING_SCREEN_TXT);
  1016.  
  1017.                             /* Create the abort requester */
  1018.  
  1019.                         if(ReqWindow = BuildEasyRequest(Window,&Easy,NULL))
  1020.                         {
  1021.                             ULONG Signals;
  1022.  
  1023.                                 /* Everything is fine so far */
  1024.  
  1025.                             Error = 0;
  1026.  
  1027.                                 /* Start printing */
  1028.  
  1029.                             BeginIO((struct IORequest *)PrintRequest);
  1030.  
  1031.                                 /* Run until everything is done */
  1032.  
  1033.                             while(TRUE)
  1034.                             {
  1035.                                     /* Wait for an event */
  1036.  
  1037.                                 Signals = Wait(PORTMASK(ReqWindow->UserPort) | PORTMASK(PrintPort));
  1038.  
  1039.                                     /* Is the printer finished? */
  1040.  
  1041.                                 if(Signals & PORTMASK(PrintPort))
  1042.                                 {
  1043.                                         /* Wait for the request to return and check for error */
  1044.  
  1045.                                     switch(WaitIO((struct IORequest *)PrintRequest))
  1046.                                     {
  1047.                                         case PDERR_NOTGRAPHICS:
  1048.  
  1049.                                             Error = ERR_NO_GFX_OUTPUT;
  1050.                                             break;
  1051.  
  1052.                                         case PDERR_BADDIMENSION:
  1053.  
  1054.                                             Error = ERR_BAD_DIMENSION;
  1055.                                             break;
  1056.  
  1057.                                         case IOERR_OPENFAIL:
  1058.  
  1059.                                             Error = ERR_NO_PRINTER;
  1060.                                             break;
  1061.  
  1062.                                         case PDERR_INTERNALMEMORY:
  1063.                                         case PDERR_BUFFERMEMORY:
  1064.  
  1065.                                             Error = ERROR_NO_FREE_STORE;
  1066.                                             break;
  1067.                                     }
  1068.  
  1069.                                     break;
  1070.                                 }
  1071.  
  1072.                                     /* Did the user press the abort button? */
  1073.  
  1074.                                 if(Signals & PORTMASK(ReqWindow->UserPort))
  1075.                                 {
  1076.                                     if(!SysReqHandler(ReqWindow,NULL,FALSE))
  1077.                                     {
  1078.                                         AbortIO((struct IORequest *)PrintRequest);
  1079.  
  1080.                                         WaitIO((struct IORequest *)PrintRequest);
  1081.  
  1082.                                         break;
  1083.                                     }
  1084.                                 }
  1085.                             }
  1086.  
  1087.                                 /* Close the requester */
  1088.  
  1089.                             FreeSysRequest(ReqWindow);
  1090.                         }
  1091.                         else
  1092.                             Error = ERROR_NO_FREE_STORE;
  1093.  
  1094.                             /* Free the memory allocated for the bitmap */
  1095.  
  1096.                         DeleteBitMap(BitMap);
  1097.                     }
  1098.                     else
  1099.                         Error = ERROR_NO_FREE_STORE;
  1100.  
  1101.                         /* Free the rastport */
  1102.  
  1103.                     FreeVecPooled(RPort);
  1104.                 }
  1105.                 else
  1106.                     Error = ERROR_NO_FREE_STORE;
  1107.  
  1108.                     /* Close the printer driver */
  1109.  
  1110.                 CloseDevice((struct IORequest *)PrintRequest);
  1111.             }
  1112.             else
  1113.                 Error = ERR_NO_PRINTER;
  1114.  
  1115.                 /* Free the rastport dump request */
  1116.  
  1117.             DeleteIORequest((struct IORequest *)PrintRequest);
  1118.         }
  1119.         else
  1120.             Error = ERROR_NO_FREE_STORE;
  1121.  
  1122.             /* Free the printer port */
  1123.  
  1124.         DeleteMsgPort(PrintPort);
  1125.     }
  1126.     else
  1127.         Error = ERROR_NO_FREE_STORE;
  1128.  
  1129.         /* Return the result */
  1130.  
  1131.     if(Error)
  1132.     {
  1133.         SetIoErr(Error);
  1134.  
  1135.         return(FALSE);
  1136.     }
  1137.     else
  1138.         return(TRUE);
  1139. }
  1140.